<template>
  <div class="data-management">
    <el-card class="page-card">
      <template #header>
        <div class="card-header">
          <span>数据接入与清洗</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>添加数据源
          </el-button>
        </div>
      </template>

      <!-- 数据源列表 -->
      <el-table :data="dataSources" style="width: 100%">
        <el-table-column prop="name" label="数据源名称" />
        <el-table-column prop="type" label="数据源类型" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'info'">
              {{ row.status === 'active' ? '已启用' : '未启用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button-group>
              <el-button type="primary" link @click="handleEdit(row)">
                编辑
              </el-button>
              <el-button type="primary" link @click="handleMapping(row)">
                字段映射
              </el-button>
              <el-button type="primary" link @click="handleRules(row)">
                清洗规则
              </el-button>
              <el-button type="danger" link @click="handleDelete(row)">
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑数据源对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加数据源' : '编辑数据源'"
      width="500px"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="数据源名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="数据源类型">
          <el-select v-model="form.type" placeholder="请选择数据源类型">
            <el-option label="MySQL" value="mysql" />
            <el-option label="PostgreSQL" value="postgresql" />
            <el-option label="MongoDB" value="mongodb" />
            <el-option label="Kafka" value="kafka" />
          </el-select>
        </el-form-item>
        <el-form-item label="连接配置">
          <el-input
            v-model="form.config"
            type="textarea"
            :rows="4"
            placeholder="请输入连接配置（JSON格式）"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 模拟数据
const dataSources = ref([
  {
    id: 1,
    name: '生产数据库',
    type: 'MySQL',
    status: 'active',
    updateTime: '2024-02-20 10:00:00'
  },
  {
    id: 2,
    name: '设备数据流',
    type: 'Kafka',
    status: 'active',
    updateTime: '2024-02-20 09:30:00'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const form = ref({
  name: '',
  type: '',
  config: ''
})

// 处理添加数据源
const handleAdd = () => {
  dialogType.value = 'add'
  form.value = {
    name: '',
    type: '',
    config: ''
  }
  dialogVisible.value = true
}

// 处理编辑数据源
const handleEdit = (row) => {
  dialogType.value = 'edit'
  form.value = { ...row }
  dialogVisible.value = true
}

// 处理字段映射
const handleMapping = (row) => {
  ElMessage.info('字段映射功能开发中...')
}

// 处理清洗规则
const handleRules = (row) => {
  ElMessage.info('清洗规则功能开发中...')
}

// 处理删除数据源
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该数据源吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 这里添加删除逻辑
    ElMessage.success('删除成功')
  })
}

// 处理表单提交
const handleSubmit = () => {
  // 这里添加表单提交逻辑
  ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
  dialogVisible.value = false
}
</script>

<style scoped>
.data-management {
  padding: 20px;
}

.page-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 